package com.yswee.framework.widget.base;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint.FontMetrics;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.Button;

public class DrawableCenterButton extends Button {
	
	public DrawableCenterButton(Context context) {
		this(context, null);
	}

	public DrawableCenterButton(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public DrawableCenterButton(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initializeView(context);
	}
	
	private void initializeView(Context context) {
		Drawable[] drawables = getCompoundDrawables();
		if (drawables[0] != null) {
			int gravity = getGravity();
			int v_gravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
			setGravity(Gravity.LEFT | v_gravity);
		} else if (drawables[1] != null) {
			int gravity = getGravity();
			int h_gravity = gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
			setGravity(Gravity.TOP | h_gravity);
		} else if (drawables[2] != null) {
			int gravity = getGravity();
			int v_gravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
			setGravity(Gravity.RIGHT | v_gravity);
		} else if (drawables[3] != null) {
			int gravity = getGravity();
			int h_gravity = gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
			setGravity(Gravity.BOTTOM | h_gravity);
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Drawable[] drawables = getCompoundDrawables();
		if (drawables[0] != null) {
			int leftDrawableWidth = drawables[0].getIntrinsicWidth();
			int drawablePadding = getCompoundDrawablePadding();
			int textWidth = (int) getPaint().measureText(getText().toString().trim());
			int bodyWidth = leftDrawableWidth + drawablePadding + textWidth;
			canvas.save();
			canvas.translate((getWidth() - bodyWidth) / 2, 0);
		} else if (drawables[1] != null) {
			int topDrawableHeight = drawables[1].getIntrinsicHeight();
			int drawablePadding = getCompoundDrawablePadding();
			FontMetrics fm = getPaint().getFontMetrics();
			int textHeight = (int) Math.ceil(fm.descent - fm.ascent);
			int bodyWidth = topDrawableHeight + drawablePadding + textHeight;
			canvas.save();
			canvas.translate(0, (getHeight() - bodyWidth) / 2);
		} else if (drawables[2] != null) {
			int rightDrawableWidth = drawables[2].getIntrinsicWidth();
			int drawablePadding = getCompoundDrawablePadding();
			int textWidth = (int) getPaint().measureText(getText().toString().trim());
			int bodyWidth = rightDrawableWidth + drawablePadding + textWidth;
			canvas.save();
			canvas.translate(-(getWidth() - bodyWidth) / 2, 0);
		} else if (drawables[3] != null) {
			int topDrawableHeight = drawables[1].getIntrinsicHeight();
			int drawablePadding = getCompoundDrawablePadding();
			FontMetrics fm = getPaint().getFontMetrics();
			int textHeight = (int) Math.ceil(fm.descent - fm.ascent);
			int bodyWidth = topDrawableHeight + drawablePadding + textHeight;
			canvas.save();
			canvas.translate(0, -(getHeight() - bodyWidth) / 2);
		}
		super.onDraw(canvas);
	}

}